1
從傳統程式碼到生成式AI應用
AI011Lesson 3
00:00

從傳統程式碼到生成式AI應用

軟體開發的格局正經歷根本性的轉變。我們正從僵化的指令驅動程式設計,轉向靈活的自然語言驅動 生成式AI 互動。

1. 打破指令鏈

這是什麼? 傳統應用程式依賴固定的圖形使用者介面(GUI)或特定、語言相關的指令集。若使用者未按預期輸入,系統就會失效。

這為什麼重要? 生成式AI應用帶來前所未有的彈性。它們允許使用者以自然語言進行互動,達成複雜目標,並根據意圖調整,而非僅僅依賴語法。

2. 非決定性原則

這是什麼? 在傳統程式碼中,$1 + 1$ 始終等於 $2$。它是決定性的。 大型語言模型(LLMs)相反地,它們則基於機率運作。

其運作方式為: 對相同的提示,它們可能產生不同的結果。這種差異由特定參數控制,其中最關鍵的是 溫度

3. 基礎單元:詞元與溫度

  • 詞元: 模型用來處理文字的基本數值「構建單元」。詞彙會被拆解為這些子詞單位。
  • 溫度: 一個設定值(範圍從 $0.0$ 到 $1.0$),用以控制隨機性。低值會產生可預測、專注的文本;高值則鼓勵創造性、多樣化的輸出。
安全第一
切勿將API金鑰直接硬編碼於應用程式碼中。應始終使用環境變數(例如 .env 檔案)來保護您的AI資源免受未經授權的存取。
app.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
Question 1
Why are Large Language Models (LLMs) described as "non-deterministic"?
Because they can produce different results for the same prompt every time.
Because they always return the exact same output for a given input.
Because they cannot run on standard computer processors.
Because they require quantum computing to function.
Question 2
Which parameter should you decrease if you want the AI output to be more predictable and less creative?
Max Tokens
Top-P
Temperature
Frequency Penalty
Challenge: Building a "Study Buddy"
Apply your knowledge to a real-world scenario.
You are building a "Study Buddy" application that must provide strictly factual definitions for students preparing for exams. The application will connect to an Azure OpenAI resource.
Task 1
Identify the optimal Temperature setting for this specific task.
Solution:
Set Temperature to 0.0 or 0.1. This minimizes randomness and ensures the model provides the most likely, factual, and consistent definitions rather than creative or hallucinated responses.
Task 2
How should you secure the application's sensitive connection data?
Solution:
Move the API_KEY from the main code file into an environment variable or a hidden .env file. Use os.getenv("AZURE_OPENAI_KEY") to retrieve it securely at runtime.